$(function() {
	// 初始化Select2控件的绑定事件
	WASP_WIDGET.triggerCompanySelect("#managerId");
	WASP_WIDGET.triggerSelectOnDicts($("#prdCategory"),{dctRootType:"PRD_CATEGORY",dctFathType:"PRD_CATEGORY"});
	WASP_WIDGET.triggerSelectOnDicts($("#operationMode"),{dctRootType:"OPERATION_MODE",dctFathType:"OPERATION_MODE"});
	WASP_WIDGET.triggerSelectOnDicts($("#collectionMethod"),{dctRootType:"COLLECTION_METHOD",dctFathType:"COLLECTION_METHOD"});
	WASP_WIDGET.triggerSelectOnDicts($("#prdBizType"),{dctRootType:"PRD_BIZ_TYPE",dctFathType:"PRD_BIZ_TYPE"});
	WASP_WIDGET.triggerInstSelect("#custodianid");
    WASP_WIDGET.triggerSelectOnDicts($("#openFrequency"),{dctRootType:"OPEN_FREQUENCY",dctFathType:"OPEN_FREQUENCY"});
	 //初始化日期控件
    WASP_WIDGET.triggerDateStyleWithYMD("#setupDate");
    $("#setupDate").on("change", function(){
		$(this).valid && $(this).valid();
	});
    WASP_WIDGET.triggerDateStyleWithYMD("#expectedEndDate");
    $("#expectedEndDate").on("change", function(){
		$(this).valid && $(this).valid();
	});
    $("#prdCategory").on("change", function(){
		$(this).valid && $(this).valid();
	});
    $("#managerId").on("change", function(){
		$(this).valid && $(this).valid();
	});
    $("#operationMode").on("change", function(){
		$(this).valid && $(this).valid();
	});
    /*$("#collectionMethod").on("change", function(){
		$(this).valid && $(this).valid();
	});*/
    $("#prdBizType").on("change", function(){
		$(this).valid && $(this).valid();
	});
    $("#custodianid").on("change", function(){
		$(this).valid && $(this).valid();
	});
    $("#openFrequency").on("change", function(){
		$(this).valid && $(this).valid();
	});
    operationModeBindChange();
    dynamicJqValidate();
});

//修改开放频率
function openFrequencyOnchange (){
	if("4" == $("#openFrequency").val()){
		$("#fixFrequencyShow").css("display","block");
		$("#fixFrequency").rules("add", {
			required:true ,number:true,messages:{required:"请输入固定频率（天）",number:"请输入数字"}
		});
	}else{
		$("#fixFrequencyShow").css("display","none");
		$("#fixFrequency").rules("remove");
	}
}

function operationModeBindChange(){
	$("#operationMode").on("change", function(){
		if($("#operationMode").val()=='0'){//0封闭式
			$("#openFrequencyBox").css("display","none");
			$("#openFrequency").rules("remove");
		}else if($("#operationMode").val()=='1'){//1开放式
			$("#openFrequencyBox").css("display","block");
			$("#openFrequency").rules("add", {
				required:true ,messages:{required:"请选择开放频率"}
			});
		}
	});
}

/**
 * 动态验证暂存和提交两个类型的数据
 * @param status
 */
function dynamicJqValidate(status) {
	$("#projectAddForm").validate({
		focusCleanup : true,
		rules : {
			prjsname :{
				required : true,
				maxlength4Byte : 100
			},
			prjlname :{
				required : true,
				maxlength4Byte : 100
			},
			prdCategory :{
				required : true
			},
			prdBizType :{
				required : true
			},
			/*prjTerm :{
				required : true,
				maxlength4Byte : 4000
			},*/
			managerId :{
				required : true
			},
			outerId:{
				required : true
			},
			/*prdRegCode :{
				required : true,
				maxlength4Byte : 50
			},*/
			/*collectionMethod :{
				required : true
			},*/
			operationMode :{
				required : true
			},
			setupDate :{
				required : true
			},
			expectedEndDate :{
				required : true
			},
			custodianid:{
				required : true
			},
			custodianleader:{
				required : true,
				maxlength4Byte : 50
			},
			custodiancontacter:{
				required : true,
				maxlength4Byte : 50
			},
			custodainphone:{
				required : true,
				maxlength4Byte : 40
			},
			custodianaddress:{
				required : true,
				maxlength4Byte : 200
			},
			/*openFrequency:{
				required : true
			},*/
			/*fixFrequency:{
				required : true
			},*/
			investTarget:{
				required : true,
				maxlength4Byte : 4000
			},
			investspec:{
				required : true,
				maxlength4Byte : 4000
			},
			openDateDesc:{
				required : true,
				maxlength4Byte : 2000
			}
		},
		messages:{
			prjsname:{
				required:'请输入产品简称',
				maxlength4Byte:'长度不能超过100'
			},
			prjlname:{
				required:'请输入产品全称',
				maxlength4Byte:'长度不能超过100'
			},
			prdCategory :{
				required : "请选择产品分类"
			},
			prdBizType :{
				required : "请选择产品业务类型"
			},
			/*prjTerm :{
				required : "请输入产品期限",
				maxlength4Byte : "长度不能超过4000"
			},*/
			managerId :{
				required : "请选择管理人公司"
			},
			outerId:{
				required : "请输入外部编码"
			},
			/*prdRegCode :{
				required : "请输入产品登记编码",
				maxlength4Byte : "长度不能超过50"
			},*/
			/*collectionMethod :{
				required : "请选择募集方式"
			},*/
			operationMode:{
				required : "请选择运作模式"
			},
			setupDate :{
				required : "请输入成立日"
			},
			expectedEndDate :{
				required : "请输入到期日"
			},
			custodianid:{
				required : "请选择托管人"
			},
			custodianleader:{
				required : "请输入托管人负责人",
				maxlength4Byte :"长度不能超过50"
			},
			custodiancontacter:{
				required : "请输入托管人联系人",
				maxlength4Byte :"长度不能超过50"
			},
			custodainphone:{
				required : "请输入托管人联系电话",
				maxlength4Byte : "长度不能超过40"
			},
			custodianaddress:{
				required : "请输入托管人地址",
				maxlength4Byte : "长度不能超过200"
			},
			/*openFrequency:{
				required : "请输入开放频率"
			},*/
			/*fixFrequency:{
				required :"请输入固定频率（天）"
			},*/
			investTarget:{
				required : "请输入投资目标",
				maxlength4Byte : "长度不能超过4000"
			},
			investspec:{
				required : "请输入投资范围及比例",
				maxlength4Byte : "长度不能超过4000"
			},
			openDateDesc:{
				required : "请输入开放时间描述",
				maxlength4Byte : "长度不能超过2000"
			}
		}
	});
	
	if (500 == status) {
		$("#prjsname").rules("remove");
		$("#prdCategory").rules("remove");
		$("#prdBizType").rules("remove");
		//$("#prjTerm").rules("remove");
		$("#managerId").rules("remove");
		//$("#prdRegCode").rules("remove");
		//$("#collectionMethod").rules("remove");
		$("#operationMode").rules("remove");
		$("#setupDate").rules("remove");
		$("#expectedEndDate").rules("remove");
		$("#custodianid").rules("remove");
		$("#custodianleader").rules("remove");
		$("#custodainphone").rules("remove");
		$("#custodianaddress").rules("remove");
		$("#openFrequency").rules("remove");
		$("#investTarget").rules("remove");
		$("#investspec").rules("remove");
		$("#openDateDesc").rules("remove");
	}
	if("4" != $("#openFrequency").val()){
		$("#fixFrequency").rules("remove");
	}
}

/**
 * 新增页面提交
 * @param status
 */
function submitPrjStartupAddForm(status) {
	//dynamicJqValidate(status);
	if(!$("#projectAddForm").valid()){
		toastr.warning('', '请完善系列产品新增信息!');
		return false;
	}
	if ($("#setupDate").val() > $("#expectedEndDate").val()) {
		toastr.warning('', '成立日不能大于到期日!');
		return false;
	}
	
	SubmitAndPreventSecond("projectAddForm",function(){});
	
}